﻿using DevExpress.XtraLayout.Utils;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Windows.Forms;
using XuanFe.AppLaunch.Common;
using XuanFe.Database.BLL;
using XuanFe.Database.DAL;
using XuanFe.Database.Model;

namespace XuanFe.AppLaunch
{
    public partial class CustomerDetailForm : DevExpress.XtraEditors.XtraForm
    {
        private const string m_ModuleId = "0201";
        private const string m_PassengerModuleId = "0202";
        private const string m_RevisitModuleId = "0203";

        // 乘机人
        private int colIndexPASSENGER_NAME = 0;
        private int colIndexMOBILE_NUMBER = 0;
        private int colIndexCUSTOMER_ID = 0;
        private int colIndexPASSENGER_ID = 0;
        private int colIndexCHINESE_SPELL = 0;
        private int colIndexID_TYPE_ID = 0;
        private int colIndexID_TYPE_NAME = 0;
        private int colIndexID_NUMBER = 0;
        private int colIndexEXPIRY_DATE = 0;
        private int colIndexNATION_ID = 0;
        private int colIndexNATION_NAME = 0;
        private int colIndexBIRTHDAY = 0;
        private int colIndexSEX = 0;
        // 回访
        private int colIndexREVISIT_TIME = 0;
        private int colIndexSTAFF_NAME = 0;
        private int colIndexSTAFF_ID = 0;
        private int colIndexREVISIT_ID = 0;
        private int colIndexREVISIT_REMARK = 0;
        // 机票订单
        private int colIndexBOOKING_ID = 0;

        private ManageState m_CustomerState;
        private ManageState m_ManageState;
        private ManageState m_RevisitState;
        private string m_CustomerId;
        private CustomerModel m_Customer;
        private List<IDTypeModel> m_IDTypes = new List<IDTypeModel>();
        private List<NationModel> m_Nations = new List<NationModel>();
        private List<ItemObject> m_Sex = new List<ItemObject>();
        private string m_PassengerId;
        private string m_RevisitId;

        public CustomerDetailForm(string customerId)
        {
            InitializeComponent();

            this.m_CustomerId = customerId;
        }
        private void SetCustomerState(ManageState state)
        {
            this.m_CustomerState = state;

            this.errorProvider1.ClearErrors();

            switch (state)
            {
                case ManageState.None:
                    this.btnModifyCustomer.Enabled = true;
                    this.btnSaveCustomer.Enabled = false;
                    this.btnCancelCustomer.Enabled = true;
                    this.btnCancelCustomer.Text = "关闭(&C)";

                    this.txtCustomerMobileNumber.Text = string.Empty;
                    this.txtCustomerName.Text = string.Empty;
                    this.txtCustomerTelephone.Text = string.Empty;
                    this.cmbCustomerSex.SelectedIndex = 0;
                    this.txtCustomerEMail.Text = string.Empty;
                    this.dtCustomerBirthday.DateTime = DateTime.Now.Date;
                    this.txtCustomerContactAddress.Text = string.Empty;

                    this.txtCustomerMobileNumber.Enabled = false;
                    this.txtCustomerName.Enabled = false;
                    this.txtCustomerTelephone.Enabled = false;
                    this.cmbCustomerSex.Enabled = false;
                    this.txtCustomerEMail.Enabled = false;
                    this.dtCustomerBirthday.Enabled = false;
                    this.txtCustomerContactAddress.Enabled = false;
                    break;
                case ManageState.Modify:
                    this.btnModifyCustomer.Enabled = false;
                    this.btnSaveCustomer.Enabled = true;
                    this.btnCancelCustomer.Enabled = true;
                    this.btnCancelCustomer.Text = "取消(&C)";

                    this.txtCustomerMobileNumber.Enabled = true;
                    this.txtCustomerName.Enabled = true;
                    this.txtCustomerTelephone.Enabled = true;
                    this.cmbCustomerSex.Enabled = true;
                    this.txtCustomerEMail.Enabled = true;
                    this.dtCustomerBirthday.Enabled = true;
                    this.txtCustomerContactAddress.Enabled = true;

                    this.txtCustomerName.Focus();
                    break;
                case ManageState.Save:
                    this.btnModifyCustomer.Enabled = false;
                    this.btnSaveCustomer.Enabled = false;
                    this.btnCancelCustomer.Enabled = false;

                    this.txtCustomerMobileNumber.Enabled = false;
                    this.txtCustomerName.Enabled = false;
                    this.txtCustomerTelephone.Enabled = false;
                    this.cmbCustomerSex.Enabled = false;
                    this.txtCustomerEMail.Enabled = false;
                    this.dtCustomerBirthday.Enabled = false;
                    this.txtCustomerContactAddress.Enabled = false;
                    break;
                case ManageState.Browse:
                case ManageState.Cancel:
                    this.btnModifyCustomer.Enabled = true;
                    this.btnSaveCustomer.Enabled = false;
                    this.btnCancelCustomer.Enabled = true;
                    this.btnCancelCustomer.Text = "关闭(&C)";

                    this.txtCustomerMobileNumber.Text = string.Empty;
                    this.txtCustomerName.Text = string.Empty;
                    this.txtCustomerTelephone.Text = string.Empty;
                    this.cmbCustomerSex.SelectedIndex = 0;
                    this.txtCustomerEMail.Text = string.Empty;
                    this.dtCustomerBirthday.DateTime = DateTime.Now.Date;
                    this.txtCustomerContactAddress.Text = string.Empty;

                    this.txtCustomerMobileNumber.Enabled = false;
                    this.txtCustomerName.Enabled = false;
                    this.txtCustomerTelephone.Enabled = false;
                    this.cmbCustomerSex.Enabled = false;
                    this.txtCustomerEMail.Enabled = false;
                    this.dtCustomerBirthday.Enabled = false;
                    this.txtCustomerContactAddress.Enabled = false;

                    this.SetCustomer();
                    break;
                default:
                    this.btnModifyCustomer.Enabled = false;
                    this.btnSaveCustomer.Enabled = false;
                    this.btnCancelCustomer.Enabled = false;

                    this.txtCustomerMobileNumber.Enabled = false;
                    this.txtCustomerName.Enabled = false;
                    this.txtCustomerTelephone.Enabled = false;
                    this.cmbCustomerSex.Enabled = false;
                    this.txtCustomerEMail.Enabled = false;
                    this.dtCustomerBirthday.Enabled = false;
                    this.txtCustomerContactAddress.Enabled = false;
                    break;
            }
        }
        /// <summary>
        /// 设置界面状态
        /// </summary>
        /// <param name="state"></param>
        private void SetManageState(ManageState state)
        {
            this.m_ManageState = state;

            this.errorProvider1.ClearErrors();

            switch (state)
            {
                case ManageState.None:
                    this.gridControl1.Enabled = true;

                    this.btnAppend.Enabled = true;
                    this.btnModify.Enabled = false;
                    this.btnDelete.Enabled = false;
                    this.btnSave.Enabled = false;
                    this.btnCancel.Enabled = false;

                    this.m_PassengerId = string.Empty;
                    this.txtPassengerMobileNumber.Text = string.Empty;
                    this.txtPassengerName.Text = string.Empty;
                    this.txtPassengerChineseSpell.Text = string.Empty;
                    this.cmbPassengerIDTypeId.SelectedIndex = 0;
                    this.txtPassengerIDNumber.Text = string.Empty;
                    this.dtPassengerExpiryDate.DateTime = DateTime.Now.Date;
                    this.cmbPassengerNation.SelectedIndex = 0;
                    this.dtPassengerBirthday.DateTime = DateTime.Now.Date;
                    this.cmbPassengerSex.SelectedIndex = 0;

                    this.txtPassengerMobileNumber.Enabled = false;
                    this.txtPassengerName.Enabled = false;
                    this.txtPassengerChineseSpell.Enabled = false;
                    this.cmbPassengerIDTypeId.Enabled = false;
                    this.txtPassengerIDNumber.Enabled = false;
                    this.dtPassengerExpiryDate.Enabled = false;
                    this.cmbPassengerNation.Enabled = false;
                    this.dtPassengerBirthday.Enabled = false;
                    this.cmbPassengerSex.Enabled = false;
                    break;
                case ManageState.Append:
                    this.gridControl1.Enabled = false;

                    this.btnAppend.Enabled = false;
                    this.btnModify.Enabled = false;
                    this.btnDelete.Enabled = false;
                    this.btnSave.Enabled = true;
                    this.btnCancel.Enabled = true;

                    this.m_PassengerId = string.Empty;
                    this.txtPassengerMobileNumber.Text = string.Empty;
                    this.txtPassengerName.Text = string.Empty;
                    this.txtPassengerChineseSpell.Text = string.Empty;
                    this.cmbPassengerIDTypeId.SelectedIndex = 0;
                    this.txtPassengerIDNumber.Text = string.Empty;
                    this.dtPassengerExpiryDate.DateTime = DateTime.Now.Date;
                    this.cmbPassengerNation.SelectedIndex = 0;
                    this.dtPassengerBirthday.DateTime = DateTime.Now.Date;
                    this.cmbPassengerSex.SelectedIndex = 0;

                    this.txtPassengerMobileNumber.Enabled = true;
                    this.txtPassengerName.Enabled = true;
                    this.txtPassengerChineseSpell.Enabled = true;
                    this.cmbPassengerIDTypeId.Enabled = true;
                    this.txtPassengerIDNumber.Enabled = true;
                    this.dtPassengerExpiryDate.Enabled = true;
                    this.cmbPassengerNation.Enabled = true;
                    this.dtPassengerBirthday.Enabled = true;
                    this.cmbPassengerSex.Enabled = true;

                    this.txtPassengerName.Focus();
                    break;
                case ManageState.Modify:
                    this.gridControl1.Enabled = false;

                    this.btnAppend.Enabled = false;
                    this.btnModify.Enabled = false;
                    this.btnDelete.Enabled = false;
                    this.btnSave.Enabled = true;
                    this.btnCancel.Enabled = true;

                    this.txtPassengerMobileNumber.Enabled = true;
                    this.txtPassengerName.Enabled = true;
                    this.txtPassengerChineseSpell.Enabled = true;
                    this.cmbPassengerIDTypeId.Enabled = true;
                    this.txtPassengerIDNumber.Enabled = true;
                    this.dtPassengerExpiryDate.Enabled = true;
                    this.cmbPassengerNation.Enabled = true;
                    this.dtPassengerBirthday.Enabled = true;
                    this.cmbPassengerSex.Enabled = true;

                    this.txtPassengerName.Focus();
                    break;
                case ManageState.Delete:
                    this.gridControl1.Enabled = false;

                    this.btnAppend.Enabled = false;
                    this.btnModify.Enabled = false;
                    this.btnDelete.Enabled = false;
                    this.btnSave.Enabled = false;
                    this.btnCancel.Enabled = false;

                    this.txtPassengerMobileNumber.Enabled = false;
                    this.txtPassengerName.Enabled = false;
                    this.txtPassengerChineseSpell.Enabled = false;
                    this.cmbPassengerIDTypeId.Enabled = false;
                    this.txtPassengerIDNumber.Enabled = false;
                    this.dtPassengerExpiryDate.Enabled = false;
                    this.cmbPassengerNation.Enabled = false;
                    this.dtPassengerBirthday.Enabled = false;
                    this.cmbPassengerSex.Enabled = false;
                    break;
                case ManageState.Save:
                    this.gridControl1.Enabled = false;

                    this.btnAppend.Enabled = false;
                    this.btnModify.Enabled = false;
                    this.btnDelete.Enabled = false;
                    this.btnSave.Enabled = false;
                    this.btnCancel.Enabled = false;

                    this.txtPassengerMobileNumber.Enabled = false;
                    this.txtPassengerName.Enabled = false;
                    this.txtPassengerChineseSpell.Enabled = false;
                    this.cmbPassengerIDTypeId.Enabled = false;
                    this.txtPassengerIDNumber.Enabled = false;
                    this.dtPassengerExpiryDate.Enabled = false;
                    this.cmbPassengerNation.Enabled = false;
                    this.dtPassengerBirthday.Enabled = false;
                    this.cmbPassengerSex.Enabled = false;
                    break;
                case ManageState.Browse:
                case ManageState.Cancel:
                    this.gridControl1.Enabled = true;

                    this.btnAppend.Enabled = true;
                    this.btnModify.Enabled = this.IsModify();
                    this.btnDelete.Enabled = this.IsDelete();
                    this.btnSave.Enabled = false;
                    this.btnCancel.Enabled = false;

                    this.m_PassengerId = string.Empty;
                    this.txtPassengerMobileNumber.Text = string.Empty;
                    this.txtPassengerName.Text = string.Empty;
                    this.txtPassengerChineseSpell.Text = string.Empty;
                    this.cmbPassengerIDTypeId.SelectedIndex = 0;
                    this.txtPassengerIDNumber.Text = string.Empty;
                    this.dtPassengerExpiryDate.DateTime = DateTime.Now.Date;
                    this.cmbPassengerNation.SelectedIndex = 0;
                    this.dtPassengerBirthday.DateTime = DateTime.Now.Date;
                    this.cmbPassengerSex.SelectedIndex = 0;

                    this.txtPassengerMobileNumber.Enabled = false;
                    this.txtPassengerName.Enabled = false;
                    this.txtPassengerChineseSpell.Enabled = false;
                    this.cmbPassengerIDTypeId.Enabled = false;
                    this.txtPassengerIDNumber.Enabled = false;
                    this.dtPassengerExpiryDate.Enabled = false;
                    this.cmbPassengerNation.Enabled = false;
                    this.dtPassengerBirthday.Enabled = false;
                    this.cmbPassengerSex.Enabled = false;

                    this.gridView1_SelectionChanged(null, null);
                    break;
                default:
                    this.gridControl1.Enabled = false;

                    this.btnAppend.Enabled = false;
                    this.btnModify.Enabled = false;
                    this.btnDelete.Enabled = false;
                    this.btnSave.Enabled = false;
                    this.btnCancel.Enabled = false;

                    this.txtPassengerMobileNumber.Enabled = false;
                    this.txtPassengerName.Enabled = false;
                    this.txtPassengerChineseSpell.Enabled = false;
                    this.cmbPassengerIDTypeId.Enabled = false;
                    this.txtPassengerIDNumber.Enabled = false;
                    this.dtPassengerExpiryDate.Enabled = false;
                    this.cmbPassengerNation.Enabled = false;
                    this.dtPassengerBirthday.Enabled = false;
                    this.cmbPassengerSex.Enabled = false;
                    break;
            }
        }
        private void SetRevisitState(ManageState state)
        {
            this.m_RevisitState = state;

            this.errorProvider1.ClearErrors();

            switch (state)
            {
                case ManageState.None:
                    this.gridControl2.Enabled = true;

                    this.btnAppendRevisit.Enabled = true;
                    this.btnSaveRevisit.Enabled = false;
                    this.btnCancelRevisit.Enabled = false;

                    this.m_RevisitId = string.Empty;
                    this.mmRevisitRemark.Text = string.Empty;

                    this.mmRevisitRemark.Enabled = false;
                    break;
                case ManageState.Append:
                    this.gridControl2.Enabled = false;

                    this.btnAppendRevisit.Enabled = false;
                    this.btnSaveRevisit.Enabled = true;
                    this.btnCancelRevisit.Enabled = true;

                    this.m_RevisitId = string.Empty;
                    this.mmRevisitRemark.Text = string.Empty;

                    this.mmRevisitRemark.Enabled = true;

                    this.mmRevisitRemark.Focus();
                    break;
                case ManageState.Modify:
                    this.gridControl2.Enabled = false;

                    this.btnAppendRevisit.Enabled = false;
                    this.btnSaveRevisit.Enabled = true;
                    this.btnCancelRevisit.Enabled = true;

                    this.mmRevisitRemark.Enabled = false;

                    this.mmRevisitRemark.Focus();
                    break;
                case ManageState.Delete:
                    this.gridControl2.Enabled = false;

                    this.btnAppendRevisit.Enabled = false;
                    this.btnSaveRevisit.Enabled = false;
                    this.btnCancelRevisit.Enabled = false;

                    this.mmRevisitRemark.Enabled = false;
                    break;
                case ManageState.Save:
                    this.gridControl2.Enabled = false;

                    this.btnAppendRevisit.Enabled = false;
                    this.btnSaveRevisit.Enabled = false;
                    this.btnCancelRevisit.Enabled = false;

                    this.mmRevisitRemark.Enabled = false;
                    break;
                case ManageState.Browse:
                case ManageState.Cancel:
                    this.gridControl2.Enabled = true;

                    this.btnAppendRevisit.Enabled = true;
                    this.btnSaveRevisit.Enabled = false;
                    this.btnCancelRevisit.Enabled = false;

                    this.m_RevisitId = string.Empty;
                    this.mmRevisitRemark.Text = string.Empty;

                    this.mmRevisitRemark.Enabled = false;

                    this.gridView2_SelectionChanged(null, null);
                    break;
                default:
                    this.gridControl2.Enabled = false;

                    this.btnAppendRevisit.Enabled = false;
                    this.btnSaveRevisit.Enabled = false;
                    this.btnCancelRevisit.Enabled = false;

                    this.mmRevisitRemark.Enabled = false;
                    break;
            }
        }

        private bool IsModify()
        {
            return (this.gridView1.RowCount > 0 && this.gridView1.FocusedRowHandle >= 0);
        }
        private bool IsModifyRevisit()
        {
            return (this.gridView2.RowCount > 0 && this.gridView2.FocusedRowHandle >= 0);
        }

        private bool IsDelete()
        {
            return (this.gridView1.RowCount > 0 && this.gridView1.FocusedRowHandle >= 0);
        }
        private bool IsDeleteRevisit()
        {
            return (this.gridView2.RowCount > 0 && this.gridView2.FocusedRowHandle >= 0);
        }

        /// <summary>
        /// 清空记录
        /// </summary>
        private void ClearRecordToGrid()
        {
            this.gridView1.BeginUpdate();
            this.gridView1.Columns.Clear();

            DevExpress.XtraGrid.Columns.GridColumn c;
            int visibleIndex = 0;

            this.colIndexPASSENGER_NAME = this.gridView1.Columns.Count;
            c = new DevExpress.XtraGrid.Columns.GridColumn();
            c.FieldName = "PASSENGER_NAME";
            c.Caption = "乘机人姓名";
            c.Fixed = DevExpress.XtraGrid.Columns.FixedStyle.Left;
            c.OptionsColumn.ReadOnly = true;
            c.OptionsColumn.AllowFocus = false;
            c.Visible = true;
            c.VisibleIndex = visibleIndex++;
            c.Summary.AddRange(new DevExpress.XtraGrid.GridSummaryItem[] {
            new DevExpress.XtraGrid.GridColumnSummaryItem(DevExpress.Data.SummaryItemType.Custom, "PASSENGER_NAME", "乘机人姓名")});
            this.gridView1.Columns.Add(c);

            this.colIndexMOBILE_NUMBER = this.gridView1.Columns.Count;
            c = new DevExpress.XtraGrid.Columns.GridColumn();
            c.FieldName = "MOBILE_NUMBER";
            c.Caption = "手机号码";
            c.Fixed = DevExpress.XtraGrid.Columns.FixedStyle.Left;
            c.OptionsColumn.ReadOnly = true;
            c.OptionsColumn.AllowFocus = false;
            c.Visible = true;
            c.VisibleIndex = visibleIndex++;
            c.Summary.AddRange(new DevExpress.XtraGrid.GridSummaryItem[] {
            new DevExpress.XtraGrid.GridColumnSummaryItem(DevExpress.Data.SummaryItemType.Count)});
            this.gridView1.Columns.Add(c);

            this.colIndexCUSTOMER_ID = this.gridView1.Columns.Count;
            c = new DevExpress.XtraGrid.Columns.GridColumn();
            c.FieldName = "CUSTOMER_ID";
            c.Caption = "客户编码";
            c.OptionsColumn.ReadOnly = true;
            c.OptionsColumn.AllowFocus = false;
            c.Visible = false;
            this.gridView1.Columns.Add(c);

            this.colIndexPASSENGER_ID = this.gridView1.Columns.Count;
            c = new DevExpress.XtraGrid.Columns.GridColumn();
            c.FieldName = "PASSENGER_ID";
            c.Caption = "乘机人编码";
            c.OptionsColumn.ReadOnly = true;
            c.OptionsColumn.AllowFocus = false;
            c.Visible = false;
            this.gridView1.Columns.Add(c);

            this.colIndexCHINESE_SPELL = this.gridView1.Columns.Count;
            c = new DevExpress.XtraGrid.Columns.GridColumn();
            c.FieldName = "CHINESE_SPELL";
            c.Caption = "全拼";
            c.OptionsColumn.ReadOnly = true;
            c.OptionsColumn.AllowFocus = false;
            c.Visible = true;
            c.VisibleIndex = visibleIndex++;
            this.gridView1.Columns.Add(c);

            this.colIndexSEX = this.gridView1.Columns.Count;
            c = new DevExpress.XtraGrid.Columns.GridColumn();
            c.FieldName = "SEX";
            c.Caption = "性别";
            c.OptionsColumn.ReadOnly = true;
            c.OptionsColumn.AllowFocus = false;
            c.Visible = false;
            this.gridView1.Columns.Add(c);

            //this.colIndexSEX = this.gridView1.Columns.Count;
            c = new DevExpress.XtraGrid.Columns.GridColumn();
            c.FieldName = "SEX_NAME";
            c.Caption = "性别";
            c.OptionsColumn.ReadOnly = true;
            c.OptionsColumn.AllowFocus = false;
            c.Visible = true;
            c.VisibleIndex = visibleIndex++;
            this.gridView1.Columns.Add(c);

            this.colIndexID_TYPE_NAME = this.gridView1.Columns.Count;
            c = new DevExpress.XtraGrid.Columns.GridColumn();
            c.FieldName = "ID_TYPE_NAME";
            c.Caption = "证件类型";
            c.OptionsColumn.ReadOnly = true;
            c.OptionsColumn.AllowFocus = false;
            c.Visible = true;
            c.VisibleIndex = visibleIndex++;
            this.gridView1.Columns.Add(c);

            this.colIndexID_TYPE_ID = this.gridView1.Columns.Count;
            c = new DevExpress.XtraGrid.Columns.GridColumn();
            c.FieldName = "ID_TYPE_ID";
            c.Caption = "证件类型编码";
            c.OptionsColumn.ReadOnly = true;
            c.OptionsColumn.AllowFocus = false;
            c.Visible = false;
            this.gridView1.Columns.Add(c);

            this.colIndexID_NUMBER = this.gridView1.Columns.Count;
            c = new DevExpress.XtraGrid.Columns.GridColumn();
            c.FieldName = "ID_NUMBER";
            c.Caption = "证件号码";
            c.OptionsColumn.ReadOnly = true;
            c.OptionsColumn.AllowFocus = false;
            c.Visible = true;
            c.VisibleIndex = visibleIndex++;
            this.gridView1.Columns.Add(c);

            this.colIndexEXPIRY_DATE = this.gridView1.Columns.Count;
            c = new DevExpress.XtraGrid.Columns.GridColumn();
            c.FieldName = "EXPIRY_DATE";
            c.Caption = "有效期";
            c.OptionsColumn.ReadOnly = true;
            c.OptionsColumn.AllowFocus = false;
            c.Visible = true;
            c.VisibleIndex = visibleIndex++;
            this.gridView1.Columns.Add(c);

            this.colIndexBIRTHDAY = this.gridView1.Columns.Count;
            c = new DevExpress.XtraGrid.Columns.GridColumn();
            c.FieldName = "BIRTHDAY";
            c.Caption = "出生日期";
            c.OptionsColumn.ReadOnly = true;
            c.OptionsColumn.AllowFocus = false;
            c.Visible = true;
            c.VisibleIndex = visibleIndex++;
            this.gridView1.Columns.Add(c);

            this.colIndexNATION_ID = this.gridView1.Columns.Count;
            c = new DevExpress.XtraGrid.Columns.GridColumn();
            c.FieldName = "NATION_ID";
            c.Caption = "国家编码";
            c.OptionsColumn.ReadOnly = true;
            c.OptionsColumn.AllowFocus = false;
            c.Visible = false;
            this.gridView1.Columns.Add(c);

            this.colIndexNATION_NAME = this.gridView1.Columns.Count;
            c = new DevExpress.XtraGrid.Columns.GridColumn();
            c.FieldName = "NATION_NAME";
            c.Caption = "签证国家";
            c.OptionsColumn.ReadOnly = true;
            c.OptionsColumn.AllowFocus = false;
            c.Visible = true;
            c.VisibleIndex = visibleIndex++;
            this.gridView1.Columns.Add(c);

            this.gridView1.EndUpdate();
        }
        private void ClearRecordToGrid2()
        {
            this.gridView2.BeginUpdate();
            this.gridView2.Columns.Clear();

            DevExpress.XtraGrid.Columns.GridColumn c;
            int visibleIndex = 0;

            this.colIndexREVISIT_ID = this.gridView2.Columns.Count;
            c = new DevExpress.XtraGrid.Columns.GridColumn();
            c.FieldName = "REVISIT_ID";
            c.Caption = "回访编码";
            c.Fixed = DevExpress.XtraGrid.Columns.FixedStyle.Left;
            c.OptionsColumn.ReadOnly = true;
            c.OptionsColumn.AllowFocus = false;
            c.Visible = true;
            c.VisibleIndex = visibleIndex++;
            c.Summary.AddRange(new DevExpress.XtraGrid.GridSummaryItem[] {
            new DevExpress.XtraGrid.GridColumnSummaryItem(DevExpress.Data.SummaryItemType.Custom, "REVISIT_ID", "总数")});
            this.gridView2.Columns.Add(c);

            this.colIndexREVISIT_TIME = this.gridView2.Columns.Count;
            c = new DevExpress.XtraGrid.Columns.GridColumn();
            c.FieldName = "REVISIT_TIME";
            c.Caption = "回访时间";
            c.Fixed = DevExpress.XtraGrid.Columns.FixedStyle.Left;
            c.OptionsColumn.ReadOnly = true;
            c.OptionsColumn.AllowFocus = false;
            c.Visible = true;
            c.VisibleIndex = visibleIndex++;
            c.Summary.AddRange(new DevExpress.XtraGrid.GridSummaryItem[] {
            new DevExpress.XtraGrid.GridColumnSummaryItem(DevExpress.Data.SummaryItemType.Count)});
            this.gridView2.Columns.Add(c);

            this.colIndexSTAFF_NAME = this.gridView2.Columns.Count;
            c = new DevExpress.XtraGrid.Columns.GridColumn();
            c.FieldName = "STAFF_NAME";
            c.Caption = "回访员工";
            c.OptionsColumn.ReadOnly = true;
            c.OptionsColumn.AllowFocus = false;
            c.Visible = true;
            c.VisibleIndex = visibleIndex++;
            this.gridView2.Columns.Add(c);

            this.colIndexSTAFF_ID = this.gridView2.Columns.Count;
            c = new DevExpress.XtraGrid.Columns.GridColumn();
            c.FieldName = "STAFF_ID";
            c.Caption = "回访员工编码";
            c.OptionsColumn.ReadOnly = true;
            c.OptionsColumn.AllowFocus = false;
            c.Visible = false;
            this.gridView2.Columns.Add(c);

            this.colIndexREVISIT_REMARK = this.gridView2.Columns.Count;
            c = new DevExpress.XtraGrid.Columns.GridColumn();
            c.FieldName = "REVISIT_REMARK";
            c.Caption = "回访内容";
            c.OptionsColumn.ReadOnly = true;
            c.OptionsColumn.AllowFocus = false;
            c.Visible = true;
            c.VisibleIndex = visibleIndex++;
            this.gridView2.Columns.Add(c);

            this.gridView2.EndUpdate();
        }
        private void ClearRecordToGrid3()
        {
            this.gridView3.BeginUpdate();
            this.gridView3.Columns.Clear();

            DevExpress.XtraGrid.Columns.GridColumn c;
            int visibleIndex = 0;

            this.colIndexBOOKING_ID = this.gridView3.Columns.Count;
            c = new DevExpress.XtraGrid.Columns.GridColumn();
            c.FieldName = "BOOKING_ID";
            c.Caption = "订单号";
            c.Fixed = DevExpress.XtraGrid.Columns.FixedStyle.Left;
            c.OptionsColumn.ReadOnly = true;
            c.OptionsColumn.AllowFocus = false;
            c.Visible = true;
            c.VisibleIndex = visibleIndex++;
            c.Summary.AddRange(new DevExpress.XtraGrid.GridSummaryItem[] {
            new DevExpress.XtraGrid.GridColumnSummaryItem(DevExpress.Data.SummaryItemType.Custom, "BOOKING_ID", "总数")});
            this.gridView3.Columns.Add(c);

            c = new DevExpress.XtraGrid.Columns.GridColumn();
            c.FieldName = "CUSTOMER_NAME";
            c.Caption = "联系人";
            c.Fixed = DevExpress.XtraGrid.Columns.FixedStyle.Left;
            c.OptionsColumn.ReadOnly = true;
            c.OptionsColumn.AllowFocus = false;
            c.Visible = true;
            c.VisibleIndex = visibleIndex++;
            c.Summary.AddRange(new DevExpress.XtraGrid.GridSummaryItem[] {
            new DevExpress.XtraGrid.GridColumnSummaryItem(DevExpress.Data.SummaryItemType.Count)});
            this.gridView3.Columns.Add(c);

            c = new DevExpress.XtraGrid.Columns.GridColumn();
            c.FieldName = "MOBILE_NUMBER";
            c.Caption = "手机号码";
            c.OptionsColumn.ReadOnly = true;
            c.OptionsColumn.AllowFocus = false;
            c.Visible = true;
            c.VisibleIndex = visibleIndex++;
            this.gridView3.Columns.Add(c);

            c = new DevExpress.XtraGrid.Columns.GridColumn();
            c.FieldName = "CLAIM_FLG";
            c.Caption = "需要送票标识";
            c.OptionsColumn.ReadOnly = true;
            c.OptionsColumn.AllowFocus = false;
            c.Visible = true;
            c.VisibleIndex = visibleIndex++;
            this.gridView3.Columns.Add(c);

            c = new DevExpress.XtraGrid.Columns.GridColumn();
            c.FieldName = "SALE_STAFF_NAME";
            c.Caption = "预订员工";
            c.OptionsColumn.ReadOnly = true;
            c.OptionsColumn.AllowFocus = false;
            c.Visible = true;
            c.VisibleIndex = visibleIndex++;
            this.gridView3.Columns.Add(c);

            c = new DevExpress.XtraGrid.Columns.GridColumn();
            c.FieldName = "SALE_TIME";
            c.Caption = "预订时间";
            c.OptionsColumn.ReadOnly = true;
            c.OptionsColumn.AllowFocus = false;
            c.Visible = true;
            c.VisibleIndex = visibleIndex++;
            c.DisplayFormat.FormatString = "yyyy-MM-dd";
            c.DisplayFormat.FormatType = DevExpress.Utils.FormatType.DateTime;
            this.gridView3.Columns.Add(c);

            c = new DevExpress.XtraGrid.Columns.GridColumn();
            c.FieldName = "BOOKING_TYPE_DESC";
            c.Caption = "预订渠道";
            c.OptionsColumn.ReadOnly = true;
            c.OptionsColumn.AllowFocus = false;
            c.Visible = true;
            c.VisibleIndex = visibleIndex++;
            this.gridView3.Columns.Add(c);

            c = new DevExpress.XtraGrid.Columns.GridColumn();
            c.FieldName = "BOOKING_STATUS_DESC";
            c.Caption = "订单状态";
            c.OptionsColumn.ReadOnly = true;
            c.OptionsColumn.AllowFocus = false;
            c.Visible = true;
            c.VisibleIndex = visibleIndex++;
            this.gridView3.Columns.Add(c);

            c = new DevExpress.XtraGrid.Columns.GridColumn();
            c.FieldName = "TOTAL_AMOUNT";
            c.Caption = "报价";
            c.OptionsColumn.ReadOnly = true;
            c.OptionsColumn.AllowFocus = false;
            c.Visible = true;
            c.VisibleIndex = visibleIndex++;
            this.gridView3.Columns.Add(c);

            c = new DevExpress.XtraGrid.Columns.GridColumn();
            c.FieldName = "TOTAL_BASE";
            c.Caption = "底价";
            c.OptionsColumn.ReadOnly = true;
            c.OptionsColumn.AllowFocus = false;
            c.Visible = true;
            c.VisibleIndex = visibleIndex++;
            this.gridView3.Columns.Add(c);

            c = new DevExpress.XtraGrid.Columns.GridColumn();
            c.FieldName = "PAID_FLG";
            c.Caption = "已支付标识";
            c.OptionsColumn.ReadOnly = true;
            c.OptionsColumn.AllowFocus = false;
            c.Visible = true;
            c.VisibleIndex = visibleIndex++;
            this.gridView3.Columns.Add(c);

            c = new DevExpress.XtraGrid.Columns.GridColumn();
            c.FieldName = "TOTAL_PAID";
            c.Caption = "已收款";
            c.OptionsColumn.ReadOnly = true;
            c.OptionsColumn.AllowFocus = false;
            c.Visible = true;
            c.VisibleIndex = visibleIndex++;
            this.gridView3.Columns.Add(c);

            c = new DevExpress.XtraGrid.Columns.GridColumn();
            c.FieldName = "COLLECTION_STAFF_NAME";
            c.Caption = "收款员工";
            c.OptionsColumn.ReadOnly = true;
            c.OptionsColumn.AllowFocus = false;
            c.Visible = true;
            c.VisibleIndex = visibleIndex++;
            this.gridView3.Columns.Add(c);

            c = new DevExpress.XtraGrid.Columns.GridColumn();
            c.FieldName = "COLLECTION_TIME";
            c.Caption = "收款时间";
            c.OptionsColumn.ReadOnly = true;
            c.OptionsColumn.AllowFocus = false;
            c.Visible = true;
            c.VisibleIndex = visibleIndex++;
            c.DisplayFormat.FormatString = "yyyy-MM-dd";
            c.DisplayFormat.FormatType = DevExpress.Utils.FormatType.DateTime;
            this.gridView3.Columns.Add(c);

            c = new DevExpress.XtraGrid.Columns.GridColumn();
            c.FieldName = "PRINT_FLG";
            c.Caption = "已出票标识";
            c.OptionsColumn.ReadOnly = true;
            c.OptionsColumn.AllowFocus = false;
            c.Visible = true;
            c.VisibleIndex = visibleIndex++;
            this.gridView3.Columns.Add(c);

            c = new DevExpress.XtraGrid.Columns.GridColumn();
            c.FieldName = "PRINT_STAFF_NAME";
            c.Caption = "出票员工";
            c.OptionsColumn.ReadOnly = true;
            c.OptionsColumn.AllowFocus = false;
            c.Visible = true;
            c.VisibleIndex = visibleIndex++;
            this.gridView3.Columns.Add(c);

            c = new DevExpress.XtraGrid.Columns.GridColumn();
            c.FieldName = "PRINT_TIME";
            c.Caption = "出票时间";
            c.OptionsColumn.ReadOnly = true;
            c.OptionsColumn.AllowFocus = false;
            c.Visible = true;
            c.VisibleIndex = visibleIndex++;
            c.DisplayFormat.FormatString = "yyyy-MM-dd";
            c.DisplayFormat.FormatType = DevExpress.Utils.FormatType.DateTime;
            this.gridView3.Columns.Add(c);

            this.gridView3.EndUpdate();
        }
        private void LoadCustomer()
        {
            this.m_Customer = CustomerDAL.Instance.Get(new QueryCustomerModel() { QueryCustomerId = true, CustomerId = this.m_CustomerId });
            this.SetCustomerState(ManageState.Browse);
        }
        private void SetCustomer()
        {
            if (this.m_Customer != null)
            {
                this.txtCustomerMobileNumber.Text = this.m_Customer.MobileNumber;
                this.txtCustomerName.Text = this.m_Customer.Name;
                this.txtCustomerTelephone.Text = this.m_Customer.Telephone;
                this.cmbCustomerSex.SelectedIndex = this.m_Customer.Sex;
                this.txtCustomerEMail.Text = this.m_Customer.EMail;
                if (this.m_Customer.Birthday != null)
                {
                    this.dtCustomerBirthday.DateTime = Convert.ToDateTime(this.m_Customer.Birthday);
                }
                this.txtCustomerContactAddress.Text = this.m_Customer.ContactAddress;
            }
        }
        private bool ModifyCustomer()
        {
            try
            {
                CustomerModel model = new CustomerModel()
                {
                    Id = this.m_CustomerId,
                    Name = this.txtCustomerName.Text.Trim(),
                    Sex = this.cmbCustomerSex.SelectedIndex,
                    Birthday = this.dtCustomerBirthday.DateTime,
                    MobileNumber = this.txtCustomerMobileNumber.Text.Trim(),
                    Telephone = this.txtCustomerTelephone.Text.Trim(),
                    EMail = this.txtCustomerEMail.Text.Trim(),
                    ContactAddress = this.txtCustomerContactAddress.Text.Trim(),
                    RegisterTime = DateTime.Now,
                    FollowStaffId = LoginInfo.Instance.StaffLogon.Staff.Id,
                    LastModifiedStaffId = LoginInfo.Instance.StaffLogon.Staff.Id,
                    LastModifiedDate = DateTime.Now,
                };

                using (DbConnection conn = MySQLHelper.Instance.CreateConnection())
                {
                    try
                    {
                        if (conn.State != ConnectionState.Open)
                            conn.Open();

                        DbTransaction tran = conn.BeginTransaction();
                        try
                        {
                            CustomerDAL.Instance.Update(tran, model);

                            tran.Commit();

                            this.m_Customer = model;
                            return (true);
                        }
                        catch
                        {
                            tran.Rollback();
                            return (false);
                        }
                    }
                    finally
                    {
                        conn.Close();
                    }
                }
            }
            catch
            {
                return (false);
            }
        }
        /// <summary>
        /// 记录是否已存在
        /// </summary>
        /// <param name="UrlCd"></param>
        /// <returns></returns>
        private bool CustomerRecordIsExist(string Code)
        {
            if (CustomerDAL.Instance.NameExist(Code))
                return (true);

            return (false);
        }
        /// <summary>
        /// 描述是否已经存在
        /// </summary>
        /// <param name="Code"></param>
        /// <param name="Desc"></param>
        /// <returns></returns>
        private bool CustomerDescriptionIsExist(string Code, string Desc)
        {
            if (CustomerDAL.Instance.Exist(Code, Desc))
                return (true);

            return (false);
        }
        /// <summary>
        /// 有效性校验
        /// </summary>
        /// <returns></returns>
        private bool ValidityCheckCustomer()
        {
            this.errorProvider1.ClearErrors();
            string message = string.Empty;
            if (Global.Instance.MandatoryFieldEmpty(this.txtCustomerName, false, ref message))
            {
                this.errorProvider1.SetError(this.txtCustomerName, message);
                this.txtCustomerName.Focus();
                return (false);
            }
            if (Global.Instance.MandatoryFieldEmpty(this.txtCustomerMobileNumber, false, ref message))
            {
                this.errorProvider1.SetError(this.txtCustomerMobileNumber, message);
                this.txtCustomerMobileNumber.Focus();
                return (false);
            }
            if (!string.IsNullOrWhiteSpace(this.txtCustomerMobileNumber.Text) && !Global.Instance.IsPhoneNumber(this.txtCustomerMobileNumber.Text))
            {
                this.errorProvider1.SetError(this.txtCustomerMobileNumber, "该字段格式不符");
                this.txtCustomerMobileNumber.Focus();
                return (false);
            }
            if (Global.Instance.MandatoryFieldEmpty(this.cmbCustomerSex, true, ref message))
            {
                this.errorProvider1.SetError(this.cmbCustomerSex, message);
                this.cmbCustomerSex.Focus();
                return (false);
            }
            if (Global.Instance.MandatoryFieldEmpty(this.txtCustomerTelephone, true, ref message))
            {
                this.errorProvider1.SetError(this.txtCustomerTelephone, message);
                this.txtCustomerTelephone.Focus();
                return (false);
            }
            if (!string.IsNullOrWhiteSpace(this.txtCustomerTelephone.Text) && !Global.Instance.IsPhoneNumber(this.txtCustomerTelephone.Text))
            {
                this.errorProvider1.SetError(this.txtCustomerTelephone, "该字段格式不符");
                this.txtCustomerTelephone.Focus();
                return (false);
            }
            if (Global.Instance.MandatoryFieldEmpty(this.txtCustomerEMail, true, ref message))
            {
                this.errorProvider1.SetError(this.txtCustomerEMail, message);
                this.txtCustomerEMail.Focus();
                return (false);
            }
            if (!string.IsNullOrWhiteSpace(this.txtCustomerEMail.Text) && !Global.Instance.IsEMail(this.txtCustomerEMail.Text))
            {
                this.errorProvider1.SetError(this.txtCustomerEMail, "该字段格式不符");
                this.txtCustomerEMail.Focus();
                return (false);
            }
            if (Global.Instance.MandatoryFieldEmpty(this.txtCustomerContactAddress, true, ref message))
            {
                this.errorProvider1.SetError(this.txtCustomerContactAddress, message);
                this.txtCustomerContactAddress.Focus();
                return (false);
            }

            return (true);
        }

        /// <summary>
        /// 加载记录
        /// </summary>
        private void LoadRecordToGrid()
        {
            using (DataSet ds = PassengerDAL.Instance.GetDataSet(this.m_CustomerId))
            {
                this.gridControl1.DataSource = ds.Tables[0];
                this.gridView1.BestFitColumns();

                if (this.gridView1.RowCount > 0)
                {
                    this.SetManageState(ManageState.Browse);
                }
                else
                {
                    this.SetManageState(ManageState.None);
                }
            }
        }
        private void BuildAll()
        {
            this.m_IDTypes = IDTypeDAL.Instance.Gets(true);
            this.m_Nations = NationDAL.Instance.Gets();
            this.m_Sex.Clear();
            this.m_Sex.AddRange(new ItemObject[] { new ItemObject("男", "M"), new ItemObject("女", "F") });

            this.cmbPassengerIDTypeId.Properties.Items.Clear();
            this.cmbPassengerIDTypeId.Properties.Items.AddRange(this.m_IDTypes);
            if (this.cmbPassengerIDTypeId.Properties.Items.Count > 0)
            {
                this.cmbPassengerIDTypeId.SelectedIndex = 0;
            }

            this.cmbPassengerNation.Properties.Items.Clear();
            this.cmbPassengerNation.Properties.Items.AddRange(this.m_Nations);


            this.cmbPassengerSex.Properties.Items.Clear();
            this.cmbPassengerSex.Properties.Items.AddRange(this.m_Sex);
            if (this.cmbPassengerSex.Properties.Items.Count != 0)
            {
                this.cmbPassengerSex.SelectedIndex = 0;
            }
        }

        /// <summary>
        /// 添加记录
        /// </summary>
        /// <returns></returns>
        private bool AppendRecord()
        {
            try
            {
                this.m_PassengerId = Guid.NewGuid().ToString("N").ToUpper();

                PassengerModel model = new PassengerModel()
                {
                    CustomerId = this.m_CustomerId,
                    PassengerId = this.m_PassengerId,
                    PassengerName = this.txtPassengerName.Text.Trim(),
                    ChineseSpell = this.txtPassengerChineseSpell.Text.Trim(),
                    MobileNumber = this.txtPassengerMobileNumber.Text.Trim(),
                    IDTypeId = ((IDTypeModel)this.cmbPassengerIDTypeId.SelectedItem).Id,
                    IDNumber = this.txtPassengerIDNumber.Text.Trim(),
                    ExpiryDate = this.dtPassengerExpiryDate.DateTime,
                    LastModifiedStaffId = LoginInfo.Instance.StaffLogon.Staff.Id,
                    LastModifiedDate = DateTime.Now,
                    Birthday = this.dtPassengerBirthday.DateTime,
                    NationId = ((NationModel)this.cmbPassengerNation.SelectedItem).Id,
                    Sex = ((ItemObject)this.cmbPassengerSex.SelectedItem).Value
                };

                using (DbConnection conn = MySQLHelper.Instance.CreateConnection())
                {
                    try
                    {
                        if (conn.State != ConnectionState.Open)
                            conn.Open();

                        DbTransaction tran = conn.BeginTransaction();
                        try
                        {
                            PassengerDAL.Instance.Insert(tran, model);

                            tran.Commit();
                            return (true);
                        }
                        catch
                        {
                            tran.Rollback();
                            return (false);
                        }
                    }
                    finally
                    {
                        conn.Close();
                    }
                }
            }
            catch
            {
                return (false);
            }
        }

        /// <summary>
        /// 修改记录
        /// </summary>
        /// <returns></returns>
        private bool ModifyRecord()
        {
            try
            {
                PassengerModel model = new PassengerModel()
                {
                    CustomerId = this.m_CustomerId,
                    PassengerId = this.m_PassengerId,
                    PassengerName = this.txtPassengerName.Text.Trim(),
                    ChineseSpell = this.txtPassengerChineseSpell.Text.Trim(),
                    MobileNumber = this.txtPassengerMobileNumber.Text.Trim(),
                    IDTypeId = ((IDTypeModel)this.cmbPassengerIDTypeId.SelectedItem).Id,
                    IDNumber = this.txtPassengerIDNumber.Text.Trim(),
                    ExpiryDate = this.dtPassengerExpiryDate.DateTime,
                    LastModifiedStaffId = LoginInfo.Instance.StaffLogon.Staff.Id,
                    LastModifiedDate = DateTime.Now,
                    Birthday = this.dtPassengerBirthday.DateTime,
                    NationId = ((NationModel)this.cmbPassengerNation.SelectedItem).Id,
                    Sex = ((ItemObject)this.cmbPassengerSex.SelectedItem).Value
                };

                using (DbConnection conn = MySQLHelper.Instance.CreateConnection())
                {
                    try
                    {
                        if (conn.State != ConnectionState.Open)
                            conn.Open();

                        DbTransaction tran = conn.BeginTransaction();
                        try
                        {
                            PassengerDAL.Instance.Update(tran, model);

                            tran.Commit();
                            return (true);
                        }
                        catch
                        {
                            tran.Rollback();
                            return (false);
                        }
                    }
                    finally
                    {
                        conn.Close();
                    }
                }
            }
            catch
            {
                return (false);
            }
        }

        /// <summary>
        /// 删除记录
        /// </summary>
        /// <param name="Code"></param>
        /// <returns></returns>
        private bool DeleteRecord(string Code)
        {
            try
            {
                using (DbConnection conn = MySQLHelper.Instance.CreateConnection())
                {
                    try
                    {
                        if (conn.State != ConnectionState.Open)
                            conn.Open();

                        DbTransaction tran = conn.BeginTransaction();
                        try
                        {
                            PassengerDAL.Instance.Delete(tran, Code);

                            tran.Commit();
                            return (true);
                        }
                        catch
                        {
                            tran.Rollback();
                            return (false);
                        }
                    }
                    finally
                    {
                        conn.Close();
                    }
                }
            }
            catch
            {
                return (false);
            }
        }

        /// <summary>
        /// 记录是否正在使用
        /// </summary>
        /// <param name="UrlCd"></param>
        /// <returns></returns>
        private bool RecordIsUsing(string Code)
        {
            return false;
        }

        /// <summary>
        /// 记录是否已存在
        /// </summary>
        /// <param name="UrlCd"></param>
        /// <returns></returns>
        private bool RecordIsExist(PassengerModel model)
        {
            if (PassengerDAL.Instance.Exist(model))
                return (true);

            return (false);
        }
        /// <summary>
        /// 获取最大的编码
        /// </summary>
        /// <param name="dataTable">数据源</param>
        /// <param name="columnName">数据源列名</param>
        /// <returns></returns>
        private string GetMaxId(DataTable dataTable, string columnName)
        {
            string result = "01";
            if (dataTable == null) return result;
            int count = dataTable.Rows.Count;

            // 获取最大的长度
            int maxlen = 0;
            for (int i = 0; i < count; i++)
            {
                string v = dataTable.Rows[i][columnName].ToString();
                if (v.Length > maxlen)
                    maxlen = v.Length;
            }

            // 获取最大长度的记录，并排序
            DataRow[] rows = dataTable.Select(string.Format("len({0})={1}", columnName, maxlen), string.Format("{0} desc", columnName));
            count = rows.Length;
            if (count == 0) return result;

            // 获取第一条记录
            string maxid = rows[0][columnName].ToString();
            if (Global.Instance.IsInt(maxid))
            {
                // 整型，直接累计并返回
                result = Convert.ToString(Convert.ToDecimal(maxid) + 1).PadLeft(maxid.Length, '0');
            }
            else
            {
                // 非整型，截取最右边为数字那部分
                string code = string.Empty;
                for (int i = maxid.Length - 1; i >= 0; i--)
                {
                    char c = maxid[i];
                    if (Char.IsNumber(c))
                    {
                        code = string.Concat(c.ToString(), code);
                        continue;
                    }
                    break;
                }
                if (!string.IsNullOrEmpty(code))
                {
                    // 成功截取最右边数字部分
                    result = string.Concat(maxid.Substring(0, maxid.Length - code.Length), Convert.ToString(Convert.ToDecimal(code) + 1).PadLeft(code.Length, '0'));
                }
                else
                {
                    // 截取失败
                    result = string.Concat(maxid, "1");
                }
            }
            return result;
        }
        /// <summary>
        /// 描述是否已经存在
        /// </summary>
        /// <param name="Code"></param>
        /// <param name="Desc"></param>
        /// <returns></returns>
        private bool DescriptionIsExist(string Code, string Desc)
        {
            if (IDTypeDAL.Instance.Exist(Code, Desc))
                return (true);

            return (false);
        }
        /// <summary>
        /// 有效性校验
        /// </summary>
        /// <returns></returns>
        private bool ValidityCheck()
        {
            this.errorProvider1.ClearErrors();
            string message = string.Empty;
            if (Global.Instance.MandatoryFieldEmpty(this.txtPassengerName, false, ref message))
            {
                this.errorProvider1.SetError(this.txtPassengerName, message);
                this.txtPassengerName.Focus();
                return (false);
            }
            if (Global.Instance.MandatoryFieldEmpty(this.txtPassengerMobileNumber, false, ref message))
            {
                this.errorProvider1.SetError(this.txtPassengerMobileNumber, message);
                this.txtPassengerMobileNumber.Focus();
                return (false);
            }
            if (this.cmbPassengerIDTypeId.Properties.Items.Count == 0 && Global.Instance.MandatoryFieldEmpty(this.cmbPassengerIDTypeId, false, ref message))
            {
                this.errorProvider1.SetError(this.cmbPassengerIDTypeId, message);
                this.cmbPassengerIDTypeId.Focus();
                return (false);
            }
            if (Global.Instance.MandatoryFieldEmpty(this.txtPassengerIDNumber, false, ref message))
            {
                this.errorProvider1.SetError(this.txtPassengerIDNumber, message);
                this.txtPassengerIDNumber.Focus();
                return (false);
            }
            if (Global.Instance.MandatoryFieldEmpty(this.cmbPassengerNation, false, ref message))
            {
                this.errorProvider1.SetError(this.cmbPassengerNation, message);
                this.cmbPassengerNation.Focus();
                return (false);
            }
            return (true);
        }

        private bool CheckSelected()
        {
            return (this.gridView1.SelectedRowsCount > 0);
        }

        private void LoadRevisitToGrid()
        {
            using (DataSet ds = CustomerRevisitDAL.Instance.GetDataSet(this.m_CustomerId))
            {
                this.gridControl2.DataSource = ds.Tables[0];
                this.gridView2.BestFitColumns();

                if (this.gridView2.RowCount > 0)
                {
                    this.SetRevisitState(ManageState.Browse);
                }
                else
                {
                    this.SetRevisitState(ManageState.None);
                }
            }
        }
        private bool AppendRevisit()
        {
            try
            {
                this.m_RevisitId = Guid.NewGuid().ToString("N").ToUpper();

                CustomerRevisitModel model = new CustomerRevisitModel()
                {
                    CustomerId = this.m_CustomerId,
                    RevisitId = this.m_RevisitId,
                    RevisitTime = DateTime.Now,
                    RevisitRemark = this.mmRevisitRemark.Text,
                    StaffId = LoginInfo.Instance.StaffLogon.Staff.Id,
                    LastModifiedStaffId = LoginInfo.Instance.StaffLogon.Staff.Id,
                    LastModifiedDate = DateTime.Now,
                };

                using (DbConnection conn = MySQLHelper.Instance.CreateConnection())
                {
                    try
                    {
                        if (conn.State != ConnectionState.Open)
                            conn.Open();

                        DbTransaction tran = conn.BeginTransaction();
                        try
                        {
                            CustomerRevisitDAL.Instance.Insert(tran, model);

                            tran.Commit();
                            return (true);
                        }
                        catch
                        {
                            tran.Rollback();
                            return (false);
                        }
                    }
                    finally
                    {
                        conn.Close();
                    }
                }
            }
            catch
            {
                return (false);
            }
        }
        private bool ValidityCheckRevisit()
        {
            this.errorProvider1.ClearErrors();
            string message = string.Empty;
            if (Global.Instance.MandatoryFieldEmpty(this.mmRevisitRemark, false, ref message))
            {
                this.errorProvider1.SetError(this.mmRevisitRemark, message);
                this.mmRevisitRemark.Focus();
                return (false);
            }

            return (true);
        }
        private void LoadBookings()
        {
            using (DataSet ds = BookingDAL.Instance.GetDataSet(new QueryBookingModel() { QueryCustomerId = true, CustomerId = this.m_CustomerId }))
            {
                this.gridControl3.DataSource = ds.Tables[0];
                this.gridView3.BestFitColumns();
            }
        }

        private void CustomerDetailForm_Load(object sender, EventArgs e)
        {
            try
            {
                this.SetCustomerState(ManageState.None);
                this.SetManageState(ManageState.None);
                this.SetRevisitState(ManageState.None);

                this.ClearRecordToGrid();
                this.ClearRecordToGrid2();
                this.ClearRecordToGrid3();

                this.BuildAll();

                this.LoadCustomer();
                this.LoadRecordToGrid();
                this.LoadRevisitToGrid();
                this.LoadBookings();

                StaffLogBLL.Instance.InsertLog(LoginInfo.Instance.StaffLogon.Staff.Id, m_ModuleId, "浏览记录");

                // 查看乘机人信息
                if (!LoginInfo.Instance.CheckPermission("020201"))
                {
                    this.lcgPassenger.Visibility = LayoutVisibility.Never;
                }
                // 查看回访记录
                if (!LoginInfo.Instance.CheckPermission("020301"))
                {
                    this.lcgRevisit.Visibility = LayoutVisibility.Never;
                }
            }
            catch (Exception ex)
            {
                Global.Instance.ShowExceptionDlg(ex);
            }
        }

        private void gridView1_SelectionChanged(object sender, DevExpress.Data.SelectionChangedEventArgs e)
        {
            try
            {
                if (this.gridView1.SelectedRowsCount != 1) return;

                int r = this.gridView1.GetSelectedRows()[0];
                if (r < 0)
                {
                    this.btnModify.Enabled = false;
                    this.btnDelete.Enabled = false;
                    return;
                }
                this.btnModify.Enabled = true;
                this.btnDelete.Enabled = true;

                this.m_PassengerId = Convert.ToString(this.gridView1.GetRowCellValue(r, this.gridView1.Columns[this.colIndexPASSENGER_ID]));
                this.txtPassengerName.Text = Convert.ToString(this.gridView1.GetRowCellValue(r, this.gridView1.Columns[this.colIndexPASSENGER_NAME]));
                this.txtPassengerMobileNumber.Text = Convert.ToString(this.gridView1.GetRowCellValue(r, this.gridView1.Columns[this.colIndexMOBILE_NUMBER]));
                this.txtPassengerChineseSpell.Text = Convert.ToString(this.gridView1.GetRowCellValue(r, this.gridView1.Columns[this.colIndexCHINESE_SPELL]));
                string idTypeId = Convert.ToString(this.gridView1.GetRowCellValue(r, this.gridView1.Columns[this.colIndexID_TYPE_ID]));
                this.cmbPassengerIDTypeId.SelectedItem = this.m_IDTypes.Find(delegate (IDTypeModel item) { return string.Equals(item.Id, idTypeId); });
                this.txtPassengerIDNumber.Text = Convert.ToString(this.gridView1.GetRowCellValue(r, this.gridView1.Columns[this.colIndexID_NUMBER]));
                this.dtPassengerExpiryDate.DateTime = Convert.ToDateTime(this.gridView1.GetRowCellValue(r, this.gridView1.Columns[this.colIndexEXPIRY_DATE]));
                string birthday = Convert.ToString(this.gridView1.GetRowCellValue(r, this.gridView1.Columns[this.colIndexBIRTHDAY]));
                if (!string.IsNullOrEmpty(birthday))
                {
                    this.dtPassengerBirthday.DateTime = Convert.ToDateTime(birthday);
                }
                string nation = Convert.ToString(this.gridView1.GetRowCellValue(r, this.gridView1.Columns[this.colIndexNATION_ID]));
                this.cmbPassengerNation.SelectedItem = this.m_Nations.Find(t => { return string.Equals(t.Id, nation); });
                string sex = Convert.ToString(this.gridView1.GetRowCellValue(r, this.gridView1.Columns[this.colIndexSEX]));
                this.cmbPassengerSex.SelectedItem = this.m_Sex.Find(t => { return string.Equals(t.Value, sex); });
            }
            catch (Exception ex)
            {
                Global.Instance.ShowExceptionDlg(ex);
            }
        }

        private void btnAppend_Click(object sender, EventArgs e)
        {
            try
            {
                this.SetManageState(ManageState.Append);
            }
            catch (Exception ex)
            {
                Global.Instance.ShowExceptionDlg(ex);
            }
        }

        private void btnModify_Click(object sender, EventArgs e)
        {
            try
            {
                if (!this.CheckSelected())
                {
                    Global.Instance.InfoDlg("没有可选的记录！");
                    return;
                }
                this.SetManageState(ManageState.Modify);
            }
            catch (Exception ex)
            {
                Global.Instance.ShowExceptionDlg(ex);
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                if (!this.CheckSelected())
                {
                    Global.Instance.InfoDlg("没有可选的记录！");
                    return;
                }

                if (!Global.Instance.QueryDlg("是否真的要删除所选的记录？")) return;

                Global.Instance.BeginWait();
                this.progressBar1.EditValue = 0;
                this.lciProgressBar.ContentVisible = true;

                int UsingCount = 0, FailedCount = 0;
                double step = 100.00 / this.gridView1.SelectedRowsCount;
                double total = 0;
                foreach (int r in this.gridView1.GetSelectedRows())
                {
                    total += step;
                    this.progressBar1.EditValue = Convert.ToInt32(Math.Round(total));
                    Application.DoEvents();

                    if (r < 0) continue;
                    string id = this.gridView1.GetRowCellValue(r, this.gridView1.Columns[this.colIndexPASSENGER_ID]).ToString();
                    if (!this.RecordIsUsing(id))
                    {
                        if (!this.DeleteRecord(id))
                        {
                            FailedCount++;
                            continue;
                        }
                        StaffLogBLL.Instance.InsertLog(LoginInfo.Instance.StaffLogon.Staff.Id, m_PassengerModuleId, string.Format("删除编码为“{0}”的记录", id));
                    }
                    else
                    {
                        UsingCount++;
                    }
                }

                if (UsingCount > 0 || FailedCount > 0)
                    Global.Instance.WarningDlg(string.Format("删除完毕！其中有{0}条记录正在使用，{1}条记录删除失败.", UsingCount, FailedCount));

                this.LoadRecordToGrid();
            }
            catch (Exception ex)
            {
                Global.Instance.ShowExceptionDlg(ex);
            }
            finally
            {
                this.progressBar1.EditValue = 100;
                this.lciProgressBar.ContentVisible = false;
                Global.Instance.EndWait();
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (!this.ValidityCheck()) return;

                Global.Instance.BeginWait();
                ManageState state = this.m_ManageState;

                string name = this.txtPassengerName.Text.Trim();
                switch (state)
                {
                    case ManageState.Append:
                        if (this.RecordIsExist(new PassengerModel() { CustomerId = this.m_CustomerId, PassengerId = string.Empty, PassengerName = name })
                            && !Global.Instance.QueryDlg(string.Format("乘机人“{0}”已经存在了，是否真的要添加？ ", name)))
                        {
                            this.errorProvider1.SetError(this.txtPassengerName, "该记录已存在！");
                            this.txtPassengerName.Focus();
                            return;
                        }

                        if (!this.AppendRecord())
                        {
                            Global.Instance.ErrorDlg("保存失败！");
                            return;
                        }
                        StaffLogBLL.Instance.InsertLog(LoginInfo.Instance.StaffLogon.Staff.Id, m_PassengerModuleId, string.Format("新增编码为“{0} - {1}”的记录", this.m_PassengerId, name));
                        break;
                    case ManageState.Modify:
                        if (this.RecordIsExist(new PassengerModel() { CustomerId = this.m_CustomerId, PassengerId = this.m_PassengerId, PassengerName = name })
                            && !Global.Instance.QueryDlg(string.Format("乘机人“{0}”已经存在了，是否真的要修改？ ", name)))
                        {
                            this.errorProvider1.SetError(this.txtPassengerName, "该记录已存在！");
                            this.txtPassengerName.Focus();
                            return;
                        }
                        if (!this.ModifyRecord())
                        {
                            Global.Instance.ErrorDlg("保存失败！");
                            return;
                        }
                        StaffLogBLL.Instance.InsertLog(LoginInfo.Instance.StaffLogon.Staff.Id, m_PassengerModuleId, string.Format("修改编码为“{0} - {1}”的记录", this.m_PassengerId, name));
                        break;
                }

                this.LoadRecordToGrid();
            }
            catch (Exception ex)
            {
                Global.Instance.ShowExceptionDlg(ex);
            }
            finally
            {
                Global.Instance.EndWait();
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.m_ManageState == ManageState.Append || this.m_ManageState == ManageState.Modify)
                {
                    this.SetManageState(ManageState.Cancel);
                    this.errorProvider1.ClearErrors();
                }
            }
            catch (Exception ex)
            {
                Global.Instance.ShowExceptionDlg(ex);
            }
        }

        private void gridView1_RowCountChanged(object sender, EventArgs e)
        {
            try
            {
                this.gridView1.SelectRow(this.gridView1.FocusedRowHandle);
                this.btnModify.Enabled = this.IsModify();
                this.btnDelete.Enabled = this.IsDelete();
            }
            catch (Exception ex)
            {
                Global.Instance.ShowExceptionDlg(ex);
            }
        }

        private void btnModifyCustomer_Click(object sender, EventArgs e)
        {
            try
            {
                this.SetCustomerState(ManageState.Modify);
            }
            catch (Exception ex)
            {
                Global.Instance.ShowExceptionDlg(ex);
            }
        }

        private void btnSaveCustomer_Click(object sender, EventArgs e)
        {
            try
            {
                if (!this.ValidityCheckCustomer()) return;

                Global.Instance.BeginWait();
                ManageState state = this.m_CustomerState;

                string id = this.m_CustomerId;
                string name = this.txtCustomerName.Text.Trim();
                string mobileNumber = this.txtCustomerMobileNumber.Text.Trim();

                switch (state)
                {
                    case ManageState.Modify:
                        if (CustomerDAL.Instance.MobileNumberExist(id, mobileNumber))
                        {
                            this.errorProvider1.SetError(this.txtCustomerMobileNumber, "该记录已存在！");
                            this.txtCustomerMobileNumber.Focus();
                            Global.Instance.WarningDlg(string.Format("电话“{0}”已经存在了", mobileNumber));
                            return;
                        }

                        if (!this.ModifyCustomer())
                        {
                            Global.Instance.ErrorDlg("保存失败！");
                            return;
                        }
                        StaffLogBLL.Instance.InsertLog(LoginInfo.Instance.StaffLogon.Staff.Id, m_ModuleId, string.Format("修改编码为“{0} - {1} - ”的记录", id, name, mobileNumber));
                        break;
                }

                this.SetCustomerState(ManageState.Browse);
            }
            catch (Exception ex)
            {
                Global.Instance.ShowExceptionDlg(ex);
            }
            finally
            {
                Global.Instance.EndWait();
            }
        }

        private void btnCancelCustomer_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.m_CustomerState == ManageState.Append || this.m_CustomerState == ManageState.Modify)
                {
                    this.SetCustomerState(ManageState.Cancel);
                    this.errorProvider1.ClearErrors();
                }
                else
                {
                    this.Close();
                }
            }
            catch (Exception ex)
            {
                Global.Instance.ShowExceptionDlg(ex);
            }
        }

        private void gridView2_SelectionChanged(object sender, DevExpress.Data.SelectionChangedEventArgs e)
        {
            try
            {
                if (this.gridView2.SelectedRowsCount != 1) return;

                int r = this.gridView2.GetSelectedRows()[0];
                if (r < 0)
                {
                    return;
                }

                this.m_RevisitId = Convert.ToString(this.gridView2.GetRowCellValue(r, this.gridView2.Columns[this.colIndexREVISIT_ID]));
                this.mmRevisitRemark.Text = Convert.ToString(this.gridView2.GetRowCellValue(r, this.gridView2.Columns[this.colIndexREVISIT_REMARK]));
            }
            catch (Exception ex)
            {
                Global.Instance.ShowExceptionDlg(ex);
            }
        }

        private void gridView2_RowCountChanged(object sender, EventArgs e)
        {
            try
            {
                this.gridView2.SelectRow(this.gridView2.FocusedRowHandle);
            }
            catch (Exception ex)
            {
                Global.Instance.ShowExceptionDlg(ex);
            }
        }

        private void tabbedControlGroup1_SelectedPageChanged(object sender, DevExpress.XtraLayout.LayoutTabPageChangedEventArgs e)
        {
            switch (e.Page.Name)
            {
                case "lcgCustomer":
                    StaffLogBLL.Instance.InsertLog(LoginInfo.Instance.StaffLogon.Staff.Id, m_ModuleId, "浏览记录");
                    break;
                case "lcgPassenger":
                    StaffLogBLL.Instance.InsertLog(LoginInfo.Instance.StaffLogon.Staff.Id, m_PassengerModuleId, "浏览记录");
                    break;
                case "lcgRevisit":
                    StaffLogBLL.Instance.InsertLog(LoginInfo.Instance.StaffLogon.Staff.Id, m_RevisitModuleId, "浏览记录");
                    break;
            }
        }

        private void btnAppendRevisit_Click(object sender, EventArgs e)
        {
            try
            {
                this.SetRevisitState(ManageState.Append);
            }
            catch (Exception ex)
            {
                Global.Instance.ShowExceptionDlg(ex);
            }
        }

        private void btnSaveRevisit_Click(object sender, EventArgs e)
        {
            try
            {
                if (!this.ValidityCheckRevisit()) return;

                Global.Instance.BeginWait();
                ManageState state = this.m_RevisitState;

                switch (state)
                {
                    case ManageState.Append:
                        if (!this.AppendRevisit())
                        {
                            Global.Instance.ErrorDlg("保存失败！");
                            return;
                        }
                        StaffLogBLL.Instance.InsertLog(LoginInfo.Instance.StaffLogon.Staff.Id, m_RevisitModuleId, string.Format("新增编码为“{0}”的记录", this.m_RevisitId));
                        break;
                }

                this.LoadRevisitToGrid();
            }
            catch (Exception ex)
            {
                Global.Instance.ShowExceptionDlg(ex);
            }
            finally
            {
                Global.Instance.EndWait();
            }
        }

        private void btnCancelRevisit_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.m_RevisitState == ManageState.Append || this.m_RevisitState == ManageState.Modify)
                {
                    this.SetRevisitState(ManageState.Cancel);
                    this.errorProvider1.ClearErrors();
                }
            }
            catch (Exception ex)
            {
                Global.Instance.ShowExceptionDlg(ex);
            }
        }

        private void btnBookingFlight_Click(object sender, EventArgs e)
        {
            try
            {
                MainForm main = this.ParentForm as MainForm;
                if (main == null)
                {
                    return;
                }

                using (DevExpress.Utils.WaitDialogForm waitdlg = new DevExpress.Utils.WaitDialogForm("正在加载...", "请稍候！"))
                {
                    EditBookingForm dlg = new EditBookingForm(this.m_Customer, null);
                    Global.Instance.ShowMdiForm<EditBookingForm>(main, dlg);
                }
            }
            catch (Exception ex)
            {
                Global.Instance.ShowExceptionDlg(ex);
            }
        }

        private void gridControl3_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                int r = this.gridView3.FocusedRowHandle;
                if (r < 0) return;

                MainForm main = this.ParentForm as MainForm;
                if (main == null)
                {
                    return;
                }

                using (DevExpress.Utils.WaitDialogForm waitdlg = new DevExpress.Utils.WaitDialogForm("正在加载...", "请稍候！"))
                {
                    string bookingId = Convert.ToString(this.gridView3.GetRowCellValue(r, this.gridView3.Columns[this.colIndexBOOKING_ID]));
                    string key = string.Concat("B", bookingId);

                    BookingDetailForm dlg = new BookingDetailForm(bookingId) { Tag = key };
                    Global.Instance.ShowMdiForm<BookingDetailForm>(main, dlg, key);
                }

            }
            catch (Exception ex)
            {
                Global.Instance.ShowExceptionDlg(ex);
            }
        }
    }
}
